<?php
/**
 * Created by PhpStorm.
 * User: Auser
 * Date: 2017/1/5
 * Time: 11:17
 */

namespace App\Services;
use XjbCancerItemService;
use TestingConfig;

class CancerXjbHelper
{
    /**格式化答案字符串
     * @param $answerArr
     * @return mixed
     */
    public function formatAnswers($answerArr){
        $relation = [];
        foreach($answerArr as $k => $v){
            $relationStr = XjbCancerItemService::getRelation($v['index']);
            if($relationStr){
                $relationArr = $this->regroup($relationStr,$v['weight'],$v['answer']);
                foreach($relationArr as $key => $value){
                    $relation[$k][$key]['index']=$value['index'];
                    $relation[$k][$key]['weight']=$value['weight'];
                    $relation[$k][$key]['answer']=$value['answer'];
                    $relations[$key] = [];
                    foreach($relation as $keys => $values){
                        foreach($values as $ke => $va){
                            if($ke == $key){
                                array_push($relations[$key],$va);
                            }
                        }
                    }
                }
            }
        }
        foreach($relations as $k => $v){
            if(!is_int($k)){
                unset($relations[$k]);
            }
        }
        return $relations;
    }


    /**不同单个测评权重或者答案替换
     * @param $answers
     * @return mixed
     */
    public function replaceFormat($answers){
        $answerArr = $this -> strToArr($answers);
        $relations = $this->formatAnswers($answerArr);
        foreach($relations as $k => $v){
            $relations = $this->checkCancer($relations);
            if($k == 4){
                foreach($v as $key => $value){
                    $height =$this->getValue($answerArr,3,'answer');
                    if($height>170){
                        $four['index']='6';
                        $four['weight']='5';
                        $four['answer']='是';
                    }else{
                        $four['index']='6';
                        $four['weight']='0';
                        $four['answer']='否';
                    }
                    if(in_array($value['index'],['6'])){
                        array_push($relations[$k],$four);
                    }
                }
            }
            if($k == 6){
                foreach($v as $key => $value){
                    if($value['index'] == 12){
                        if($value['weight'] == -5){
                            $relations[$k][$key]['weight']=-10;
                        }
                    }
                }
            }
            if($k == 5){
                foreach($v as $key => $value){
                    if($value['index'] == 7){
                        if($value['weight'] == -5){
                            $relations[$k][$key]['weight']=-10;
                        }
                    }
                }
            }
            if($k == 9){
                foreach($v as $key => $value){
                    if($value['index'] == 7){
                        if($value['weight'] == -5){
                            $relations[$k][$key]['weight']=-10;
                        }
                    }
                    if($value['index'] == 13){
                        if($value['weight'] == 5){
                            $relations[$k][$key]['weight']=10;
                        }
                    }
                }
            }
        }
    return $relations;
    }

    /**替换单项不同部分
     * @param $relations
     * @return mixed
     */
    public function checkCancer($relations){
        foreach($relations as $k => $v){
            $arr = ['5','6','7','8','9','10','11'];
            if(in_array($k,$arr)){
                foreach($v as $key => $value){
                    if($value['index'] == 5){
                        $str = $value['answer'];
                        $res = $this->cutStr($str);
                        if(!empty($res)){
                                if(in_array($k,$res)){
                                    $relations[$k][$key]['weight']='10';
                                    $relations[$k][$key]['answer']=$this->getTitle($k);
                                }else{
                                    $relations[$k][$key]['weight']='0';
                                    $relations[$k][$key]['answer']='无';
                                }

                        }else{
                            $relations[$k][$key]['weight']='0';
                            $relations[$k][$key]['answer']='无';
                        }
                    }
                }
            }else{
                foreach($v as $key => $value){
                    foreach($v as $key => $value){
                        if($value['index'] == 4){
                            $str = $value['answer'];
                            $res = $this->cutStr($str);
                            if(!empty($res)){
                                if(in_array($k,$res)){
                                    $relations[$k][$key]['weight']='10';
                                    $relations[$k][$key]['answer']=$this->getTitle($k);
                                }else{
                                    $relations[$k][$key]['weight']='0';
                                    $relations[$k][$key]['answer']='无';
                                }
                            }else{
                                $relations[$k][$key]['weight']='0';
                                $relations[$k][$key]['answer']='无';
                            }
                        }
                    }
                }
            }
        }
        return $relations;

    }

    /**格式化映射关系
     * @param $str
     * @param $weight
     * @param $answer
     * @return array
     */
    public function regroup($str,$weight,$answer){
        $relation = explode(',',$str);
        $data = [];
        foreach($relation as $k => $v){
            $tid_index = explode('-',$v);
            $data[$tid_index[0]]['index']=$tid_index[1];
            $data[$tid_index[0]]['weight']=$weight;
            $data[$tid_index[0]]['answer']=$answer;
        }
        return $data;
    }

    /**多选癌症分配到单项
     * @param $str
     * @return array
     */
    public function cutStr($str){
        $data = [];
        $arr = explode('|||',$str);
        $realitonStr = TestingConfig::getValue('parter.xjb.cancer.relation');
        $realitonarr = explode(',',$realitonStr);
        foreach($realitonarr as $k => $v){
            $res = explode('-',$v);
            foreach($arr as $key => $value){
                if($value == $res[0]){
                    $data[$key]=$res[1];
                }
            }
        }
        return  $data;
    }

    /**根据单项获取测评title
     * @param $tid
     * @return mixed
     */
    public function getTitle($tid){
        $realitonStr = TestingConfig::getValue('parter.xjb.cancer.relation');
        $realitonarr = explode(',',$realitonStr);
        foreach($realitonarr as $k => $v){
                $res = explode('-',$v);
                if($tid == $res[1]){
                    $str = $res[0];
                }
        }
        return $str;
    }

    /**格式化返回结果
     * @param $data
     */
    public function formatResult($data){
        $conclusion = [];
        $extra = '';
       $datas = [];
        foreach($data as $k => $v){
            $data[$k]['title']=$this->getTitle($k);
            $data[$k]['grade']=$this->getGrade($v['result']);
            $conclusion[$k]=$v['result'];
            $extra = $v['extra'];
            unset($data[$k]['extra']);
            array_push($datas,$data[$k]);
        }
        $datas = $this -> sortArr($datas);
        $result['extra']=$extra;
        //$result['composite']=$this->getComposite($conclusion);
        $result['list']=$datas;
        return $result;
    }

    /**根据等级字段排序
     * @param $datas
     */
    public function sortArr($datas){
        $grades = [];
        foreach($datas as $k => $v){
            $grades[$k]=$v['grade'];
        }
        array_multisort($grades, SORT_DESC, $datas);
        return $datas;
    }

    /**根据字符串获取等级
     * @param $res
     * @return int
     */
    public function getGrade($res){
        $grade = 0;
        if('极高风险' == $res){
            $grade = 5;
        }elseif('较高风险'  == $res){
            $grade = 4;
        }elseif('中等风险' == $res){
            $grade = 3;
        }elseif('较低风险' == $res){
            $grade = 2;
        }elseif('极低风险' == $res){
            $grade = 1;
        }
        return $grade;
    }

    /**返回综合等级
     * @param $res
     * @return array
     */
    public function getComposite($res){
        $count = array_count_values($res);
        $score = array(
            '极高风险'=>0,
            '较高风险'=>0,
            '中等风险'=>0,
            '较低风险'=>0,
            '极低风险'=>0
        );
        foreach($count as $k => $v){
            foreach($score as $key => $value){
                if($k == $key){
                    $score[$key]=$v;
                }
            }
        }
        if($score['极高风险'] >=4 || $score['较高风险'] >=4 || $score['极高风险']+$score['较高风险'] >= 4){
            $str = "极高风险";
        }elseif($score['极高风险'] == 0 || $score['较高风险'] == 0){
            $str = "中等风险";
        }elseif($score['较低风险'] == 8 || $score['极低风险'] == 8 || $score['较低风险']+$score['极低风险'] == 8){
            $str = "较低风险";
        }else{
            $str = "较高风险";
        }
        return $str;
    }

    /**将答案数组组装成字符串
     * @param $answers
     * @return string
     */
    public function arrToStr($answers)
    {
        $arr = [];
        foreach ($answers as $k => $v) {
            $arr[0] = $v['index'];
            $arr[1] = $v['weight'];
            $arr[2] = $v['answer'];
            $str = implode(":", $arr);
            $answers[$k] = $str;
        }
        $answerStr = implode("##", $answers);
        return $answerStr;
    }


    /***将答案字符串转换为数组
     * @param $answerStr
     * @return array
     */
    public function strToArr($answerStr){
        $answers = [];
        $answerArr = explode('##',$answerStr);
        if(count($answerArr)>0){
            foreach ($answerArr as $answer){
                $singleArr = explode(':',$answer);
                if(count($singleArr)>0){
                    $obj['index'] = $singleArr[0];
                    $obj['weight'] = $singleArr[1];
                    $obj['answer'] = $singleArr[2];
                    array_push($answers,$obj);
                }
            }
        }
        return $answers;
    }

    /**根据题号获取相应的权重或者值
     * @param $answers
     * @param $index
     * @param $valueType
     * @return null
     */
    public function getValue($answers,$index,$valueType){
        $value = null;
        foreach($answers as $k => $v){
            if($v['index'] == $index){
                $value = $answers[$k][$valueType];
            }
        }
        return $value;
    }








}